package com.dmxh.controller;

import com.dmxh.pojo.TbSeller;
import com.dmxh.result.Result;
import com.dmxh.service.SellerService;
import com.dmxh.vo.PasswordVo;
import com.dmxh.vo.SellerVo;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    @PostMapping("/queryByName")
    public TbSeller queryByName(@RequestBody String name) {
        return sellerService.queryByName(name);
    }

    //修改资料回显
    @GetMapping("/getByName")
    public Result getByName(HttpServletRequest request) {
        String username = request.getHeader("username");
        TbSeller tbSeller = sellerService.queryByName(username);
        SellerVo sellerVo = new SellerVo();
        BeanUtils.copyProperties(tbSeller, sellerVo);
        return Result.ok().setData(sellerVo);
    }

    @PostMapping("/updateInfo")
    public Result updateInfo(@RequestBody SellerVo sellerVo) {
        sellerService.updateInfo(sellerVo);
        return Result.ok();
    }

    //注册商家账号
    @PostMapping("/register")
    public Result register(@RequestBody TbSeller tbSeller) {
        sellerService.register(tbSeller);
        return Result.ok();
    }

    //修改密码
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody PasswordVo passwordVo, HttpServletRequest request) {
        String oldpass = passwordVo.getOldpass();
        String pass = passwordVo.getPass();
        String username = request.getHeader("username");
        return sellerService.updatePassword(username, pass, oldpass);
    }


    /*接口:测试用*/
    @GetMapping("/queyById")//此接口无用
    public Result queryByIdVoid(Integer id) {
        return Result.ok().setData(id+"我在sellerController");
    }
}
